package com.kaikeba.controller;

import com.kaikeba.bean.*;
import com.kaikeba.mvc.ResponseBody;
import com.kaikeba.service.CourierExpressService;
import com.kaikeba.service.UserExpressService;
import com.kaikeba.util.DateFormatUtil;
import com.kaikeba.util.JSONUtil;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

public class CourierExpressController {

    @ResponseBody("/courier/console.do")
    public String console(HttpServletRequest request, HttpServletResponse response){
        Map<String, Integer> console = CourierExpressService.console();
        Message msg = new Message();
        if(console.size() == 0){
            //没有查询到
            msg.setStatus(-1);
        }else {
            msg.setStatus(0);
        }
        msg.setData(console);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/list.do")
    public String list(HttpServletRequest request, HttpServletResponse response){
        //获取参数值
        int offset = Integer.parseInt(request.getParameter("offset"));
        int pageNumber = Integer.parseInt(request.getParameter("pageNumber"));
        List<CourierExpress> all = CourierExpressService.findAll(true, offset, pageNumber);
        List<BootStrapTableCourierExpress> list2 = new ArrayList<>();
        //遍历
        for (CourierExpress e : all) {
            String createtime = DateFormatUtil.format(e.getCreateTime());
            String logintime = e.getLoginTime() == null?"-":DateFormatUtil.format(e.getLoginTime());
            BootStrapTableCourierExpress es = new BootStrapTableCourierExpress(e.getId(),e.getUsername(),e.getUserPhone(),e.getIDCard(),e.getPassword(),e.getCount(),createtime,logintime);
            list2.add(es);
        }
        //获取总数
        Integer data_size = CourierExpressService.console().get("data_size");
        ResultData<BootStrapTableCourierExpress> data = new ResultData<>();
        data.setRows(list2);
        data.setTotal(data_size);
        String json = JSONUtil.toJSON(data);
        return json;
    }

    @ResponseBody("/courier/find.do")
    public String findByPhone(HttpServletRequest request, HttpServletResponse response){
        //获取前台传来的参数
        String userPhone = request.getParameter("userPhone");
        //调用service方法
        CourierExpress data = CourierExpressService.findByPhone(userPhone);
        Message msg = new Message();
        if(data != null){
            //查到值了
            msg.setStatus(0);
            msg.setResult("查询成功");
        }else {
            //没查到或者其他原因
            msg.setStatus(-1);
            msg.setResult("查询失败");
        }
        msg.setData(data);
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/update.do")
    public String update(HttpServletRequest request, HttpServletResponse response){
        //获取参数值
        int id = Integer.parseInt(request.getParameter("id"));
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String idCard = request.getParameter("IDCard");
        String password = request.getParameter("password");
        //判断手机号和身份证号有没有重复的
        CourierExpress byPhone = CourierExpressService.findByPhone(userPhone);
        CourierExpress byIdCard = CourierExpressService.findByIdCard(idCard);
        //创建Message方法
        Message msg = new Message();
        if((byPhone != null && byPhone.getId() != id) || (byIdCard != null && byIdCard.getId() != id)) {
            //证明有重复的
            msg.setStatus(-1);
            msg.setResult("修改失败,手机号或身份证号重复");
        }else {
            //建立一个快递员方法
            CourierExpress e = new CourierExpress(username, userPhone, idCard, password);
            //调用service方法
            boolean flag = CourierExpressService.update(id, e);
            if (flag) {
                //修改成功
                msg.setStatus(0);
                msg.setResult("修改成功");
            } else {
                //修改失败
                msg.setStatus(-1);
                msg.setResult("修改失败");
            }
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }


    @ResponseBody("/courier/delete.do")
    public String delete(HttpServletRequest request, HttpServletResponse response){
        //获取参数
        int id = Integer.parseInt(request.getParameter("id"));
        //调用service方法
        boolean flag = CourierExpressService.delete(id);
        //创建Message方法
        Message msg = new Message();
        if(flag){
            //修改成功
            msg.setStatus(0);
            msg.setResult("删除成功");
        }else{
            //修改失败
            msg.setStatus(-1);
            msg.setResult("删除失败");
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

    @ResponseBody("/courier/insert.do")
    public String insert(HttpServletRequest request, HttpServletResponse response){
        //获取前端传过来的值
        String username = request.getParameter("username");
        String userPhone = request.getParameter("userPhone");
        String id_card = request.getParameter("id_card");
        String password = request.getParameter("password");
        //判断手机号与身份证号有没有重复的
        CourierExpress byPhone = CourierExpressService.findByPhone(userPhone);
        CourierExpress byIdCard = CourierExpressService.findByIdCard(id_card);
        Message msg = new Message();
        if(byPhone == null && byIdCard == null) {
            CourierExpress e = new CourierExpress(username, userPhone, id_card, password);
            boolean insert = CourierExpressService.insert(e);
            if (insert) {
                msg.setResult("注册成功！");
                msg.setStatus(0);
            }else {
                msg.setResult("抱歉,注册失败~");
                msg.setStatus(-1);
            }
        }else {
            msg.setResult("抱歉,注册失败~,手机号或身份证号已被注册");
            msg.setStatus(-1);
        }
        String json = JSONUtil.toJSON(msg);
        return json;
    }

}
